package jmu.dao.impl;

import jmu.dao.RevertDAO;
import jmu.db.ConnectionManager;
import jmu.vo.Revert;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class RevertDAOImpl implements RevertDAO {

    @Override
    public Revert findById(int id) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Revert revert = new Revert();
        try {
            con = ConnectionManager.getConnection();
            // 修正SQL条件字段为revertID
            String sql = "SELECT * FROM revert WHERE revertID = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, id); // 设置查询参数
            rs = pstmt.executeQuery();
            if (rs.next()) {
                revert.setRevertID(rs.getInt("revertID"));
                revert.setMessageID(rs.getInt("messageID"));
                revert.setContent(rs.getString("content"));
                revert.setWriter(rs.getString("writer"));
                revert.setWriteDate(rs.getDate("writeDate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeResultSet(rs);
            ConnectionManager.closeStatement(pstmt);
            ConnectionManager.closeConnection(con);
        }
        return revert;
    }

    @Override
    public List<Revert> findAllRevertByMessageID(int messageID) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Revert> revertList = new ArrayList<>();
        try {
            con = ConnectionManager.getConnection();
            String sql = "SELECT * FROM revert WHERE messageID = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, messageID);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Revert revert = new Revert();
                revert.setRevertID(rs.getInt("revertID"));
                revert.setMessageID(rs.getInt("messageID"));
                revert.setContent(rs.getString("content"));
                revert.setWriter(rs.getString("writer"));
                revert.setWriteDate(rs.getDate("writeDate"));
                revertList.add(revert);
            }
        } catch (SQLException | NumberFormatException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeResultSet(rs);
            ConnectionManager.closeStatement(pstmt);
            ConnectionManager.closeConnection(con);
        }
        return revertList;
    }

    @Override
    public boolean insertRevert(Revert revert) {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean success = false;
        try {
            con = ConnectionManager.getConnection();
            String sql = "INSERT INTO revert (messageID, content, writer, writeDate) VALUES (?, ?, ?, ?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, revert.getMessageID());
            pstmt.setString(2, revert.getContent());
            pstmt.setString(3, revert.getWriter());
            // 转换java.util.Date到java.sql.Date
            pstmt.setDate(4, new java.sql.Date(revert.getWriteDate().getTime()));
            int rowsAffected = pstmt.executeUpdate();
            success = rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeStatement(pstmt);
            ConnectionManager.closeConnection(con);
        }
        return success;
    }

    @Override
    public boolean updateRevert(Revert revert) {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean success = false;
        try {
            con = ConnectionManager.getConnection();
            String sql = "UPDATE revert SET messageID=?, content=?, writer=?, writeDate=? WHERE revertID=?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, revert.getMessageID());
            pstmt.setString(2, revert.getContent());
            pstmt.setString(3, revert.getWriter());
            pstmt.setDate(4, new java.sql.Date(revert.getWriteDate().getTime()));
            pstmt.setInt(5, revert.getRevertID());
            int rowsAffected = pstmt.executeUpdate();
            success = rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeStatement(pstmt);
            ConnectionManager.closeConnection(con);
        }
        return success;
    }

    @Override
    public boolean deleteRevert(int id) {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean success = false;
        try {
            con = ConnectionManager.getConnection();
            String sql = "DELETE FROM revert WHERE revertID = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, id);
            int rowsAffected = pstmt.executeUpdate();
            success = rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeStatement(pstmt);
            ConnectionManager.closeConnection(con);
        }
        return success;
    }

    @Override
    public boolean deleteRevertsByMessageID(Connection con, int messageID) throws Exception {
        PreparedStatement pstmt = null;
        try {
            String sql = "DELETE FROM revert WHERE messageID = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, messageID);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected >= 0; // 返回true即使没有回复被删除
        } catch (SQLException e) {
            throw new SQLException("Failed to delete reverts by message ID", e);
        } finally {
            ConnectionManager.closeStatement(pstmt);
        }
    }
}